from django.shortcuts import render

# Create your views here.
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import pnp.models as models
import pnp.cfg as cfg
import os
import sys
import json

@csrf_exempt
def testRegistrationInfo(request):
    js = models.getRegistrationInfo({})
    return JsonResponse({'code':0,'data':js})

@csrf_exempt
def getRegistrationInfo(request):
    try:
        json_result = json.loads(request.body)
        js = models.getRegistrationInfo(json_result)
        return JsonResponse({'code':0,'data':js})
    except:
        return JsonResponse({'code': -1})
@csrf_exempt
def uploadfile(request):
    print(request.POST)   #<QueryDict: {'csrfmiddlewaretoken': ['Cng1EW8TV1sbsknKXTQfsJHeGkvStPI33d9KNBkkur39tsuG68xROGmyZ9hZkcap'], 'user': ['lilz']}>
    print(request.FILES)   #<MultiValueDict: {'file_obj': [<InMemoryUploadedFile: 1.doc (application/msword)>]}>
    file_obj=request.FILES.get('file') #获取到文件对象

    # 文件对象有一个name属性，获取文件名称字符串
    print(file_obj.name)
    path = os.path.join(cfg.UploadPath,file_obj.name)

    with open(path,"wb") as f:  #写入文件
        for line in file_obj:
            f.write(line)

    return JsonResponse({'code':0})